package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.result.Result;
import com.sky.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author ghy
 * @version 1.0.1
 * @date 2023-04-27 15:03:50
 */
@Service
@Slf4j
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    @Override
    public Result add(ShoppingCartDTO shoppingCartDTO) {
        //1.先判断当前加入的菜或者套餐在购物车中是否存在？
        //select * from shopping_cart where dish_id = ? and setmeal_id = ? and dish_flavor = ? and user_id = ?
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setUserId(BaseContext.getCurrentId());

        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        ShoppingCart shoppingCartDB = shoppingCartMapper.getItem(shoppingCart);
        //2.不存在，直接增加
        if(shoppingCartDB == null) {
            log.warn("购物车中不存在这个菜" );
            //1.1 是如何判断加入的是菜还是套餐。dish_id不为空，加的就是菜， setmeal_id不为空，加的就是套餐
            if(shoppingCartDTO.getDishId() != null) {
                log.warn("dish_id不为空，加的就是菜" );
                //dish_id不为空，加的就是菜
                Dish dish = dishMapper.getById(shoppingCartDTO.getDishId());
                shoppingCart.setDishId(shoppingCartDTO.getDishId());
                shoppingCart.setDishFlavor(shoppingCartDTO.getDishFlavor());
                if(dish != null) {
                    shoppingCart.setName(dish.getName());
                    shoppingCart.setAmount(dish.getPrice());  //要查
                    shoppingCart.setImage(dish.getImage());  //要查
                }

            } else {
                log.warn("setmeal_id不为空，加的就是套餐" );
                //setmeal_id不为空，加的就是套餐
                Setmeal setmeal = setmealMapper.getById(shoppingCartDTO.getSetmealId());
                shoppingCart.setSetmealId(shoppingCartDTO.getSetmealId());
                if(setmeal != null) {
                    shoppingCart.setName(setmeal.getName());
                    shoppingCart.setAmount(setmeal.getPrice());  //要查
                    shoppingCart.setImage(setmeal.getImage());  //要查
                }
            }
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartMapper.insert(shoppingCart);
        } else {
            //3.存在，更新数量
            log.warn("购物车中已经存在了，更新数量" );
            shoppingCartDB.setNumber(shoppingCartDB.getNumber() + 1);
            shoppingCartDB.setCreateTime(LocalDateTime.now());
            shoppingCartMapper.update(shoppingCartDB);
        }
        return Result.success();
    }

    @Override
    public Result<List<ShoppingCart>> list() {
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.getByUserId(BaseContext.getCurrentId());
        return Result.success(shoppingCartList);
    }

    @Override
    public Result clean() {
        shoppingCartMapper.deleteByUserId(BaseContext.getCurrentId());
        return Result.success();
    }
}
